package com.jielihaofeng.chat.controller;

import com.jielihaofeng.chat.entity.ChatMessageEntity;
import com.jielihaofeng.chat.query.ChatMessageQuery;
import com.jielihaofeng.chat.service.ChatMessageService;
import com.jielihaofeng.chat.vo.ChatMessageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.jielihaofeng.framework.common.page.PageResult;
import com.jielihaofeng.framework.common.utils.Result;
import com.jielihaofeng.chat.convert.ChatMessageConvert;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* 消息表
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2022-07-22
*/
@RestController
@RequestMapping("chat/chatmessage")
@Tag(name="消息表")
@AllArgsConstructor
public class ChatMessageController {
    private final ChatMessageService chatMessageService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('chat:chatmessage:page')")
    public Result<PageResult<ChatMessageVO>> page(@Valid ChatMessageQuery query){
        PageResult<ChatMessageVO> page = chatMessageService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('chat:chatmessage:info')")
    public Result<ChatMessageVO> get(@PathVariable("id") Long id){
        ChatMessageEntity entity = chatMessageService.getById(id);

        return Result.ok(ChatMessageConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('chat:chatmessage:save')")
    public Result<String> save(@RequestBody ChatMessageVO vo){
        chatMessageService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('chat:chatmessage:update')")
    public Result<String> update(@RequestBody @Valid ChatMessageVO vo){
        chatMessageService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('chat:chatmessage:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        chatMessageService.delete(idList);

        return Result.ok();
    }
}